package uk.co.iceroad.twu.m1;

import java.util.ArrayList;
import java.util.List;

public class PrimeFactors {

    public List<Integer> getPrimeFactors(int number) {
        List<Integer> factors = new ArrayList<Integer>();

        //Check 2 separately to increase efficiency
        if(number % 2 == 0)
            factors.add(2);

        //Start from three and go up in 2s as we already know if the number is even
        for(int i = 3; i < number; i+=2){
            if(number % i == 0 && isPrime(i)){
                factors.add(i);
            }
        }
        return factors;
    }

    private boolean isPrime(int num) {
        //Only need to check up to num/2, as after that is just the inverse
        for(int i = 2; i <= num/2 ; i++){
           if(num % i == 0)
               return false;
        }
        return true;
    }
}
